package com.efesco.service.archives;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.bstek.dorado.common.*;
import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.view.*;
import com.bstek.dorado.view.data.*;
import com.bstek.dorado.view.control.*;
import com.efesco.common.CommUtil;

/**
 * V_change_in_deal_addViewModel
 */
public class V_change_in_deal_addViewModel extends DefaultViewModel {
	protected void initDatasets() throws Exception {
		super.initDatasets();
		// Add your code here
	}

	protected void initControls() throws Exception {
		super.initControls();
		// Add your code here
	}

	protected void doLoadData() throws Exception {

		Dataset dataset = this.getDataset("dataset_change_in_detail");

		dataset.insertRecord();
		Record viewRecord = dataset.getCurrent();
		viewRecord.setValue("HOUSEHOLDER_NAME", CommUtil
				.getUserConfigValue("rpr_householder_name"));
		viewRecord.setValue("ADRESS", CommUtil
				.getUserConfigValue("rpr_HOMEPLACE"));

		super.doLoadData();
	}

	protected void doUpdateData(ParameterSet parameters) throws Exception {
		Dataset dataset = getDataset("dataset_change_in_detail");
		Dataset datas = getDataset("dataset_change_in_list");
		DBStatement dbs = new DBStatement();
		// CommUtil.testDataset(datas);

		datas.moveFirst();
		// dataset.moveFirst();
		// System.out.println("===="+datas.getCurrent());
		while (!datas.isLast()) {
			if (datas.getDate("EMPLOYEE_BIRTHDAY") == null)
				continue;
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			String sql = "update s_employee set employee_birthday=to_date('"
					+ df.format(datas.getDate("EMPLOYEE_BIRTHDAY"))
					+ "','yyyy-MM-dd'),marriage_status="
					+ datas.getString("MARRIAGE_STATUS") + ",employee_gender="
					+ datas.getString("EMPLOYEE_GENDER")
					+ " where employee_id=" + datas.getValue("EMPLOYEE_ID");
			dbs.setSql(sql);
			dbs.execute();
			datas.moveNext();
		}

		// CommUtil.testDataset(dataset);
		while (!dataset.isLast()) {
			if (dataset.getRecordState() == Record.STATE_NEW) {
				// dataset.setLong("ARCHIVER_RPR_ID",
				// Long.valueOf(CommUtil.getNewId("general")).longValue());
				dataset.setString("INPUT_NAME", CommUtil.getEmployeeId());
				dataset.setDate("REGISTER_DATE", new Date());
			}
			dataset.moveNext();
		}
		dbs.close();
		// if(dataset.getValue("STATUS")!=null&&dataset.getInt("STATUS")==1){
		// dbs.setSql("update s_in_char_service_transaction set status=2 where
		// in_char_service_transaction_id="+dataset.getValue("IN_CHAR_SERVICE_TRANSACTION_ID"));
		// dbs.execute();
		// }
		// super.doUpdateData(parameters);
		CommUtil.doUpdateData(dataset, "s_archives_rpr", "ARCHIVER_RPR_ID");
		// CommUtil.doUpdateData(datas,"s_employee","employee_id");
	}
}
